using System.Collections.Generic;
using FireStarterModeller.BO;
using FireStarterModeller.Test.Logic;
using FireStarterModeller.UI;
using FireStarterModeller.UI.ViewEditor;
using Habanero.Faces.Base;
using Habanero.Faces.Win;
using Habanero.Util;
using NUnit.Framework;
using Rhino.Mocks;

namespace FireStarterModeller.Test.UI
{
    [TestFixture]
    public class TestDMClassDetailEditorControl
    {
        [TestFixtureSetUp]
        public void SetupFixture()
        {
            UITestUtils.SetupFixture();
        }


        [SetUp]
        public virtual void SetupTest()
        {
            UITestUtils.SetupTest();

        }

        private static IControlFactory GetControlFactory()
        {
            return GlobalUIRegistry.ControlFactory;
        }

        
        [Test]
        public void Test_CreateControl()
        {
            //---------------Set up test pack-------------------
            IControlFactory controlFactory = GlobalUIRegistry.ControlFactory;
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            DMClassDetailEditorControl control = CreateDMClassDetailEditorControl(controlFactory);
            //---------------Test Result -----------------------
            Assert.IsNotNull(control);
            Assert.IsNotNull(control.ControlFactory);
            Assert.AreSame(controlFactory, control.ControlFactory);
        }

        private static DMClassDetailEditorControl CreateDMClassDetailEditorControl()
        {
            return CreateDMClassDetailEditorControl(GetControlFactory());
        }
        private static DMClassDetailEditorControl CreateDMClassDetailEditorControl(IControlFactory controlFactory)
        {
            FireStarterFormsController formsController = new FireStarterFormsControllerStub(controlFactory);
            return new CrossThreadRunner().RunInSTA(() =>
                new DMClassDetailEditorControl(controlFactory, formsController));
        }

        [Test]
        public void Test_Layout_TabControl()
        {
            //---------------Set up test pack-------------------
            IControlFactory controlFactory = GlobalUIRegistry.ControlFactory;
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            DMClassDetailEditorControl control = CreateDMClassDetailEditorControl(controlFactory);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, control.Controls.Count);
            Assert.IsInstanceOf(typeof(ITabControl), control.Controls[0]);
            ITabControl tabControl = (ITabControl)control.Controls[0];
            Assert.AreEqual(2, tabControl.TabPages.Count);
            ITabPage dmTabPage = tabControl.TabPages[0];
            Assert.AreEqual("Domain Model", dmTabPage.Text);
            ITabPage uiTabPage = tabControl.TabPages[1];
            Assert.AreEqual("UI Model", uiTabPage.Text);
        }

        [Test]
        public void Test_Layout_DMTabPage()
        {
            //---------------Set up test pack-------------------
            IControlFactory controlFactory = GlobalUIRegistry.ControlFactory;
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            DMClassDetailEditorControl control = CreateDMClassDetailEditorControl(controlFactory);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, control.Controls.Count);
            ITabControl tabControl = (ITabControl)control.Controls[0];
            ITabPage dmTabPage = tabControl.TabPages[0];
            Assert.AreEqual("Domain Model", dmTabPage.Text);
            Assert.AreEqual(1, dmTabPage.Controls.Count);
            Assert.IsInstanceOf(typeof(PanelWin), dmTabPage.Controls[0]);
            PanelWin panelWin = (PanelWin)dmTabPage.Controls[0];
            Assert.IsTrue(panelWin.AutoScroll);
            Assert.AreEqual(1, panelWin.Controls.Count);
            Assert.IsInstanceOf(typeof(CollapsiblePanelGroupControlWin), panelWin.Controls[0]);
        }

        [Test]
        public void Test_Layout_UITabPage()
        {
            //---------------Set up test pack-------------------
            IControlFactory controlFactory = GlobalUIRegistry.ControlFactory;
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            DMClassDetailEditorControl control = CreateDMClassDetailEditorControl(controlFactory);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, control.Controls.Count);
            ITabControl tabControl = (ITabControl)control.Controls[0];
            ITabPage uiTabPage = tabControl.TabPages[1];
            Assert.AreEqual("UI Model", uiTabPage.Text);
            Assert.AreEqual(1, uiTabPage.Controls.Count);
            Assert.IsInstanceOf(typeof(IUIViewsEditor), uiTabPage.Controls[0]);
            Assert.AreSame(uiTabPage.Controls[0], control.UIViewsEditor);
//            PanelWin panelWin = (PanelWin)uiTabPage.Controls[0];
//            Assert.IsTrue(panelWin.AutoScroll);
//            Assert.AreEqual(1, panelWin.Controls.Count);
//            Assert.IsInstanceOf(typeof(CollapsiblePanelGroupControlWin), panelWin.Controls[0]);
        }

        [Test]
        public void Test_HasClassInfoDetailsCollapsiblePanel()
        {
            //---------------Set up test pack-------------------
            DMClassDetailEditorControl panelGroupControl = CreateDMClassDetailEditorControl();

            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            List<ICollapsiblePanel> collapsiblePanels = panelGroupControl.PanelList;
            //---------------Test Result -----------------------
            Assert.IsNotNull(collapsiblePanels);
            Assert.AreEqual(4, collapsiblePanels.Count);
            Assert.IsInstanceOf(typeof(DMClassEditorControl), collapsiblePanels[0].ContentControl);
            Assert.AreEqual("Entities/Classes", collapsiblePanels[0].CollapseButton.Text);
        }

        [Test]
        public void Test_HasPropertyInfoCollapsiblePanel()
        {
            //---------------Set up test pack-------------------
            DMClassDetailEditorControl panelGroupControl = CreateDMClassDetailEditorControl();

            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            List<ICollapsiblePanel> collapsiblePanels = panelGroupControl.PanelList;
            //---------------Test Result -----------------------
            Assert.IsNotNull(collapsiblePanels);
            Assert.AreEqual(4, collapsiblePanels.Count);
            Assert.IsInstanceOf(typeof(DMPropertyAndRuleEditorControl), collapsiblePanels[1].ContentControl);
            Assert.AreEqual("Properties", collapsiblePanels[1].CollapseButton.Text);
        }

        [Test]
        public void Test_HasRelationshipInfoCollapsiblePanel()
        {
            //---------------Set up test pack-------------------
            DMClassDetailEditorControl panelGroupControl = CreateDMClassDetailEditorControl();

            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            List<ICollapsiblePanel> collapsiblePanels = panelGroupControl.PanelList;
            //---------------Test Result -----------------------
            Assert.IsNotNull(collapsiblePanels);
            Assert.AreEqual(4, collapsiblePanels.Count);
            Assert.IsInstanceOf(typeof(DMRelationshipsControl), collapsiblePanels[2].ContentControl);
            Assert.AreEqual("Relationships", collapsiblePanels[2].CollapseButton.Text);

        }

        [Test]
        public void Test_HasUniqueConstraintsCollapsiblePanel()
        {
            //---------------Set up test pack-------------------
            DMClassDetailEditorControl panelGroupControl = CreateDMClassDetailEditorControl();

            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            List<ICollapsiblePanel> collapsiblePanels = panelGroupControl.PanelList;
            //---------------Test Result -----------------------
            Assert.IsNotNull(collapsiblePanels);
            Assert.AreEqual(4, collapsiblePanels.Count);
            Assert.IsInstanceOf(typeof(DMUniqueConstraintAndPropertyEditorControl), collapsiblePanels[3].ContentControl);
            Assert.AreEqual("Unique Constraints", collapsiblePanels[3].CollapseButton.Text);

        }

        [Test]
        public void Test_HasClassDetailAndPropertiesPanelsPinned()
        {
            //---------------Set up test pack-------------------
            DMClassDetailEditorControl panelGroupControl = CreateDMClassDetailEditorControl();
            List<ICollapsiblePanel> collapsiblePanels = panelGroupControl.PanelList;
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            ICollapsiblePanel classDetailPanel = collapsiblePanels[0];
            ICollapsiblePanel propertiesPanel = collapsiblePanels[1];
            //---------------Test Result -----------------------
            Assert.IsTrue(classDetailPanel.Pinned);
            Assert.IsTrue(propertiesPanel.Pinned);
            Assert.IsFalse(collapsiblePanels[2].Pinned);
            Assert.IsFalse(collapsiblePanels[3].Pinned);

        }

        [Test]
        public void Test_DmClass_WhenSet_ShouldSetBusinessObjectOnDMClassEditorControl()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            DMClassDetailEditorControl classDetailEditorControl = CreateDMClassDetailEditorControl();

            //---------------Execute Test ----------------------
            classDetailEditorControl.DMClass = dmClass;
            //---------------Test Result -----------------------
            Assert.AreSame(dmClass, classDetailEditorControl.DMClassEditorControl.BusinessObject);
        }

        [Test]
        public void Test_DmClass_WhenSet_ShouldNotSetBusinessObjectOnUIViewEditorControl()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
            DMClassDetailEditorControl classDetailEditorControl = CreateDMClassDetailEditorControl();
            IUIViewsEditor uiViewsEditor = MockRepository.GenerateStub<IUIViewsEditor>();
            classDetailEditorControl.UIViewsEditor = uiViewsEditor;
            //---------------Execute Test ----------------------
            classDetailEditorControl.DMClass = dmClass;
            //---------------Test Result -----------------------
            uiViewsEditor.AssertWasNotCalled(editor => editor.EditUIViews(dmClass));
        }

        [Test]
        public void TestDmClassSelected_SetsBusinessObjectOnPropertiesControl()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            DMClassDetailEditorControl panelGroupControl = CreateDMClassDetailEditorControl();

            //---------------Execute Test ----------------------
            panelGroupControl.DMClass = dmClass;
            //---------------Test Result -----------------------
            Assert.AreSame(dmClass, panelGroupControl.DMPropertyAndRuleEditorControl.Class);
        }

        [Test]
        public void TestBusinessObjectSelected_SetsBusinessObjectOnRelationshipsControl()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            DMClassDetailEditorControl panelGroupControl = CreateDMClassDetailEditorControl();

            //---------------Execute Test ----------------------
            panelGroupControl.DMClass = dmClass;
            //---------------Test Result -----------------------
            Assert.AreSame(dmClass, panelGroupControl.DMRelationshipsControl.Class);
        }

        [Test]
        public void TestBusinessObjectSelected_SetsBusinessObjectOnUniqueConstraintsControl()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            DMClassDetailEditorControl panelGroupControl = CreateDMClassDetailEditorControl();

            //---------------Execute Test ----------------------
            panelGroupControl.DMClass = dmClass;
            //---------------Test Result -----------------------
            Assert.AreSame(dmClass, panelGroupControl.UniqueConstraintPropertyEditorControl.Class);

        }

        [Test]
        public void Test_SetSolution_ShouldSetSolutionInRelationshipControl()
        {
            //---------------Set up test pack-------------------
            DMSolution dmSolution = TestUtilsShared.CreateUnsavedValidDMSolution();
            DMClassDetailEditorControl panelGroupControl = CreateDMClassDetailEditorControl();

            //---------------Assert Precondition----------------
            Assert.IsNull(panelGroupControl.Solution);
            //---------------Execute Test ----------------------
            panelGroupControl.Solution = dmSolution;
            //---------------Test Result -----------------------
            DMRelationshipSelectorControl selector = (DMRelationshipSelectorControl) panelGroupControl.DMRelationshipsControl.RelationshipSelector;
            Assert.AreSame(dmSolution, selector.Solution);
        }

        [Test]
        public void Test_ResetSolution_WithNoClasses_ShouldSetDMClassInPanelToNull()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            DMSolution solutionNoClasses = TestUtilsShared.CreateUnsavedValidDMSolution();
            var dmSolution = dmClass.Solution;
            DMClassDetailEditorControl panelGroupControl = CreateDMClassDetailEditorControl();
            DMClassEditorControl classEditorControl = panelGroupControl.DMClassEditorControl;
            panelGroupControl.Solution = (DMSolution) dmSolution;
            panelGroupControl.DMClass = dmClass;
            //---------------Assert Precondition----------------
            Assert.AreSame(dmSolution, panelGroupControl.Solution);
            Assert.AreSame(dmClass, panelGroupControl.DMClass);
            Assert.AreSame(dmClass, classEditorControl.BusinessObject);
            Assert.IsTrue(classEditorControl.Enabled);
            Assert.AreEqual(0, solutionNoClasses.AllClasses.Count);
            //---------------Execute Test ----------------------
            panelGroupControl.Solution = solutionNoClasses;
            //---------------Test Result -----------------------
            Assert.AreSame(solutionNoClasses, panelGroupControl.Solution);
            Assert.IsNull(panelGroupControl.DMClass);
            Assert.IsNull(classEditorControl.BusinessObject);
        }
    }

}
